package com.xtianzhuang.www.controller.project;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VProjectIntf;
import com.xtianzhuang.www.entity.VProjectIntfExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteProjectInterfaceRequest;
import com.xtianzhuang.www.entity.request.ProjectDetailQueryRequest;
import com.xtianzhuang.www.entity.request.SaveProjectInterfaceRequest;
import com.xtianzhuang.www.entity.response.DeleteProjectInterfaceResponse;
import com.xtianzhuang.www.entity.response.ProjectDetailQueryResponse;
import com.xtianzhuang.www.entity.response.SaveProjectInterfaceResponse;
import com.xtianzhuang.www.utils.PageData;

/**
 * 
 * @author xtian.zhuang
 * @date Jul 22, 2019
 */
@Controller
@RequestMapping("/project")
public class ProjectInterfaceController extends BaseController {


	@RequestMapping("/ajaxQueryProjectInterface")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public ProjectDetailQueryResponse queryProjectInterface(ProjectDetailQueryRequest request) {
		ProjectDetailQueryResponse response = new ProjectDetailQueryResponse();
		VProjectIntfExample example = new VProjectIntfExample();
		if (StringUtils.isEmpty(request.getKeyword())) {
			example.or().andProjectIdEqualTo(request.getProjectId());
		} else {
			VProjectIntfExample.Criteria criteria = example.createCriteria();
			criteria.andProjectIdEqualTo(request.getProjectId()).andInterfaceCodeLike("%" + request.getKeyword() + "%");
			VProjectIntfExample.Criteria criteria2 = example.createCriteria();
			criteria2.andProjectIdEqualTo(request.getProjectId())
					.andInterfaceNameLike("%" + request.getKeyword() + "%");
			example.or(criteria2);
		}
		example.setOrderByClause("show_order asc,interface_id asc");
		PageData<VProjectIntf> data = commonServiceBiz.getPageData(example);
		data.getData().add(new VProjectIntf());
		response.setCode(CodeConstants.SUCCESS);
		response.setData(data.getData());
		response.setTotal(data.getTotal());
		return response;

	}

	@RequestMapping("/ajaxSaveInterface")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	@RequiresPermissions(value = {"interface-add", "interface-update"}, logical = Logical.OR)
	public SaveProjectInterfaceResponse saveProjectInterface(SaveProjectInterfaceRequest request) {
		SaveProjectInterfaceResponse response = new SaveProjectInterfaceResponse();
		try {
			if (request.getInterfaceId() == null) {
				projectServiceBiz.addProjectInterface(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("添加成功");
			} else {
				projectServiceBiz.updateProjectInterface(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("修改成功");
			}

		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

	@RequestMapping("/ajaxDeleteInterface")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	@RequiresPermissions("interface-delete")
	public DeleteProjectInterfaceResponse deleteProjectInterafce(DeleteProjectInterfaceRequest request) {
		DeleteProjectInterfaceResponse response = new DeleteProjectInterfaceResponse();
		try {
			projectServiceBiz.deleteProjectInterface(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

}
